var url = "/area";

var setting = {
    async: { //异步加载
        enable: true, //[setting.async.enable = true 时生效]
        url: url,
        autoParam: ["CHANNEL_ID", "SITE_CODE"],//异步加载时需要自动提交父节点属性的参数。
        dataFilter: filterd//用于对 Ajax 返回数据进行预处理的函数。
    },
    check: {//单选
        enable: true,
        //chkStyle: "radio",
        radioType: "all"//all范围整棵树level每一级
    },
    callback: {
        onClick: clickDom,
        beforeCheck: beforeCheck,
        onCheck: onCheck
    },
    data: {
        key:{
            name:"CHANNEL_NAME"
        },
        simpleData: {
            enable: false,
            idKey: "CHANNEL_ID",
            pIdKey: "PARENT_ID",
        },
        render: function(name,treeNode){
            return name+treeNode.name
        }

    },
    checkable: false //每个节点上是否显示 CheckBox
};
var zTree;
var treeNodes;
$(function () {
    //初始化树
    areaZtreeData();
});
//初始化节点
$(document).ready(function () {
    $.fn.zTree.init($("#demotree"), setting, treeNodes);
});

function clickDom(){

}
//初始化树数组
function areaZtreeData() {
    $.ajax({
        async: false,
        cache: false,
        type: 'POST',
        dataType: "json",
        url: url,//请求的action路径
        data: {"id": 'fa744f49edec4671b3f82986dae033c8'},
        error: function () {
            //请求失败处理函数
            alert('请求失败');
        },
        success: function (data) {
            //请求成功后处理函数。
            var treeNodesTemp = eval(data); //把后台封装好的简单Json格式赋给treeNodes
            treeNodes = treeNodesTemp.data.page.list;
        }
    });
}

//数据处理
function filterd(treeId, parentNode, childNodes) {
    if (!childNodes) return null;
    for (var i = 0, l = childNodes.length; i < l; i++) {
        childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
    }
    return childNodes;
}

//触发点击事件
function onCheck(e, treeId, treeNode) {
    $('#id').val(treeNode.id);
}

//禁止点击
function beforeCheck(treeId, treeNode) {
    console.log(treeId+"" + treeNode);
    //可以进行部分逻辑判断
    return true;
}
